package view.itemRenderes
{
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.ui.Keyboard;
	
	import mx.controls.Alert;
	import mx.events.FlexEvent;
	
	import org.flashIso.engine.base.IsoMap;
	import org.flashIso.engine.events.IsoEvent;
	import org.flashIso.engine.library.objects.LObject;
	import org.flashIso.engine.library.objects.LTile;
	import org.flashIso.engine.objects.IsoObject;
	import org.flashIso.engine.objects.IsoTile;
	
	import spark.events.TextOperationEvent;
	
	
	public class ItemRendererLTile extends ItemRendererLibraryItemBase
	{
		private var object:IsoTile;
		public var isoMap:IsoMap;
		
		public function ItemRendererLTile()
		{
			trace("ItemRendererLTile.constructor " + this.parent);
			super();
			addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler, false, 0, true);
			textInput.addEventListener(FocusEvent.FOCUS_OUT, hideTextInput, false, 0, true);
			textInput.addEventListener(MouseEvent.CLICK, textInputClick, false, 0, true);
			addEventListener(MouseEvent.DOUBLE_CLICK, showTextInput, false, 0, true);
			object = new IsoTile();
		}
		private function keyDownHandleer(e:KeyboardEvent) : void {
			if (e.keyCode == Keyboard.ENTER){
				hideTextInput();
			}
		}
		
		override public function set data(value:Object):void {
			super.data = value;
			if (data != null){
				object.libraryReference = data as LTile;
			}
			
		}
		
		protected function creationCompleteHandler(event:FlexEvent):void
		{
			isoMap = new IsoMap();
			container.addChild(isoMap);
			
			
			object.addEventListener(IsoEvent.DRAW, objectDrawn, false, 0, true);
			object.drawHeight = true;
			object.libraryReference = data as LTile;
			isoMap.addElement(object);
			addEventListener(MouseEvent.DOUBLE_CLICK, showTextInput, false, 0, true);
			(data as LTile).addEventListener(Event.CHANGE, objectChange, false, 0, true);
			
			objectChange();
		}
		private function objectChange(e:Event = null): void {
			llabel.text = textInput.text = data.id;
		}
		private function objectDrawn(e:Event) : void {
			isoMap.x = 0;
			isoMap.y = 0;
			
			isoMap.scaleX = isoMap.scaleY = 1;
			isoMap.scaleX = isoMap.scaleY = Math.min(this.width/isoMap.width, this.height/isoMap.height, 1);
			
			var rect:Rectangle = isoMap.getBounds(container);
			isoMap.x = -(isoMap.width / 2) - rect.x;
			isoMap.y = -(isoMap.height / 2) - rect.y;
		}
		
		
		protected function textInputClick(event:MouseEvent):void
		{
			event.stopPropagation();
		}
		private function showTextInput(e:Event = null) : void {
			textInput.visible = true;
			textInput.text = llabel.text;
			textInput.getFocus();
			stage.addEventListener(MouseEvent.CLICK, hideTextInput, false, 0, true);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandleer, false, 0, true);
		}
		
		protected function hideTextInput(event:Event = null):void
		{
			if (textInput.visible == true){
				stage.removeEventListener(MouseEvent.CLICK, hideTextInput);
				stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandleer);
				textInput.visible = false;
				if (llabel.text != textInput.text){
					dispatchEvent(new Event(Event.CHANGE));
				}
			}
		}
	}
}